package com.kolon.business.mOI.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIJudgeCheckController")
public class MOIJudgeCheckController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIJudgeCheck.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map model = new HashMap();
		String result = "N";
		try {
			// 도메인
			String url = MsgUtil.getMessage("xml.url");
			// 회원정보 세션
			HttpSession httpSession = request.getSession();
			Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
			String userId = (String)userMap.get("userId");
			String userPw = (String)userMap.get("userPw");
			String cpyCD = (String)userMap.get("CompanyCD");
			String deptCode = (String)userMap.get("DeptCode");
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/oi_suggestion.aspx?strGubun=selJudge&mode=judmanager&Page=1&PageFlag=e1");
			buffer.append("&companycd=").append(cpyCD);
			buffer.append("&UserID=&UserName=&DeptCode=").append(deptCode);
			buffer.append("&DeptName=&HeadFlag=&CLJudgeYN=N&AdminYN=N&EnvironmentYN=N&ViewRowCount=50&search1=&search2=&search_txt=");
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("Admin_Jud_Manager_T");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				if ("T".equals(el.getChild("headflag").getText()) || "D".equals(el.getChild("headflag").getText())) {
					result = "Y";
					break;
				}
			}
		} catch (Exception e) {
			result = "N";
		}
		model.put("result",result);
		return new ModelAndView("jsonView", model);
	}

}
